<template>
  <div>
    <div class="node" v-has="{action: 'dept:site:list', effect: 'move'}">
    <div class="search_box topSelect">
      <div class="selectBox">
        <label>站点：</label>
        <el-select
          v-model="searchs.siteId"
          clearable
          @change="getRate"
          placeholder="请选择"
        >
          <el-option  v-for="item of brandList"
            :key="item.id"
            :value="item.id"
            :label="item.name"></el-option>
        </el-select>
      </div>
      <div class="selectBox">
        <label>部门：</label>
        <el-select
          v-model="searchs.deptId"
          clearable
          @change="getRate"
          placeholder="请选择"
        >
          <el-option  v-for="item of deptList"
            :key="item.id"
            :value="item.id"
            :label="item.name"></el-option>
        </el-select>
      </div>
      <div class="searchBox1" style="margin-left: 20px">
        <el-button size="small" plain icon="el-icon-search" @click="getRate">查询</el-button>        
      </div>
    </div>
    <Panel title="部门站点管理" useBack>
      <div slot='ext' class="examine">
        <!-- <div class="adopt" @click="()=>editStore()">新增</div> -->
    </div>
    <TableEncapsulation
      slot="body"
      :tableConfig="tableConfig"
      :tableData="tableData"
    >
    <template slot="operation" slot-scope="operation">
          <!-- <el-button
            type="text"
            @click="()=>editStore(operation.row.id)"
          >编辑</el-button> -->
          <el-button size="mini" type="danger" icon="el-icon-delete"
            v-has="{action: 'dept:site:delete', effect: 'move'}"
            @click="()=>remove(operation.row.id)"
          >删除</el-button>
        </template>
        <TablePager
          slot="pager"
          v-if="pager.use"
          :size="pager.size"
          :pageChange="pageChange"
          :show-total="pager.showTotal"
          :total="pager.total"
          :current="pager.current"
        >
          <span class="demonstration">GO></span>
        </TablePager>
    </TableEncapsulation>
    </Panel>
    </div>
    <div style="display: none;width:100%;height:100%;margin: auto;" v-has="{action: 'dept:site:list', effect: 'noPremiss'}">
      <NoPremission></NoPremission>
    </div>
  </div>
</template>
<script>
import request from "@/tools/request";
import Panel from "@/components/Panel";
import TableEncapsulation from "@/components/TableEncapsulation";
import TablePager from "@/components/Pager";
import SearchInput from "@/components/SearchInput";
import { objectFilter } from "@/tools/array.js"
export default {
  name: "deptSite",
  components: {
    TableEncapsulation,
    TablePager,
    Panel,
    SearchInput
  },
  data() {
    return {
      searchs:{
        component: 'deptSite',
        name:'',
        siteId:"",
        deptId:'',
      },
      tableData: [],
       pager: {
        component: 'deptSite',
        use: true,
        total: 0,
        current: 1,
        size: 10,
        showTotal: true
      },
      deptList: [{id:'',name:'请选择'}],
      operation: true,
      brandList:[{id:'',name:'请选择'}],
    };
  },
  created() {
    if(this.$route.query.id){
      this.searchs.deptId = this.$route.query.id
    }
    this.searchsFnc()
    this.getRate()
    this.iotSite()
    this.rate()
  },
  computed: {
    tableConfig () {
      return {
        selection: true,
        columnConfig: [
             {
            label: "部门",
            type: "text",
            prop: "deptName",
            align: "center"
          },
          {
            label: "站点",
            type: "text",
            prop: "siteName",
            align: "center"
          },
          this.operation && {
            type: "operation",
            align: "center",
            label: "操作",
            width:180
          }
        ].filter(Boolean)
        }
    }
  },
  methods: {
     searchsFnc (){
      let searchs = this.$store.getters.searchs
      if(searchs &&searchs.component == 'deptSite'){
        this.searchs = searchs
      }
      let pager = this.$store.getters.pager
      if(pager &&pager.component == 'deptSite'){
        this.pager = pager
      }
    },
    async getRate() {
      let dept = this.searchs.deptId, page = this.pager.current, site = this.searchs.siteId
      let seach = objectFilter({ dept, page, site})
      const res = await request({
        url: "/dept/site/list",
        method: "POST",
        data: seach
      });
      this.tableData = res.page.list
      this.pager.total = res.page.totalCount
      this.pager.current = res.page.currPage
      
    },
    pageChange(val) {
      this.pager.current = val;
      this.getRate();
    },
    changeSearch(key, val) {
      this[key] = val;
      this.pager.current = 1;
    },
    editStore(id){
      this.$store.commit('saveSearchs', this.searchs)
      this.$store.commit('savePager', this.pager)
      this.$router.push({ path: "/rate/map/info",query:{id:id} })
    },
    commentDetail(id){
      this.$router.push({ path: "/storeDetail",query:{id:id,type: "toview"} })
    },
    //获取站点
    async iotSite(){
      let { list } = await request({
      url: "/iot/site/select",
      method: "POST",
      data: {}
      });
      this.brandList.push(...list) 
    },
    //部门
    async rate(){
      let { list } = await request({
      url: "/sys/dept/select",
      method: "GET",
      data: {}
      });
      this.deptList.push(...list) 
    },
    remove(id){
       this.$confirm('确认删除吗', '操作确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then( async () => {
        let del = await request({
        url: "/dept/site/delete",
        method: "POST",
        dataType4:'json',
        data: [id]
      });
      if(del.code !==0) return
        this.$message.success('删除成功')
        if(this.tableData.length===1 && this.pager.current>1)
          this.$set(this.pager,'current', this.pager.current-1)
        this.getRate()
      })
    },
  }
}
</script>
<style lang='scss' module>
.examine{
    display: flex;
    align-items: center;
    :global{
        div{
            color: #fff;
            padding: 10px;
            margin: 0 10px;
            border-radius: 2px;
            font-size: 14px;
            cursor: pointer;
            min-width: 60px;
            text-align: center;
        }
        .adopt{
                background: #409EFF;
        }
    }
}
.search_box {
  display: flex;
  align-items: center;
  flex-wrap: wrap;

  :global {
    .el-input {
      width: 150px;
    }
    .el-select {
      .el-input {
        width: 90px;
      }
    }
    .el-input__icon {
      line-height: 27px;
    }
    .el-range-separator{
      line-height: 26px;
    }
  }
}

.topSelect {
  background-color: white;
  border-radius: 4px;
  padding: 20px;
  margin-bottom: 20px;
}
</style>